package com.ll.studentservice.controller;


import com.ll.commonUtils.MD5;
import com.ll.commonUtils.R;
import com.ll.studentservice.entity.EduClass;
import com.ll.studentservice.entity.EduStudent;
import com.ll.studentservice.entity.vo.AddStudent;
import com.ll.studentservice.service.EduClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ll
 * @since 2020-11-26
 */
@Api(description = "班级信息增删改查API") //swagger界面提示
@RestController
@RequestMapping("/studentservice/educlass")
public class EduClassController {

    @Autowired
    private EduClassService eduClassService;

    @ApiOperation("添加班级信息")
    @PostMapping("addClass")
    public R addClass(@RequestBody EduClass eduClass) {

        boolean save = eduClassService.save(eduClass);

        //三元运算符
        return save ? R.ok() : R.error();
    }

    @ApiOperation("删除班级信息")
    @DeleteMapping("deleteClass/{classId}")
    public R deleteClass(@PathVariable String classId){
        boolean b = eduClassService.removeById(classId);
        return b ? R.ok() : R.error();
    }

    @ApiOperation("查询所有的班级信息")
    @GetMapping("findAllClass")
    public R findAllClass() {
        List<EduClass> eduClasses = eduClassService.list(null);
        return R.ok().data("allClass", eduClasses);
    }

    @ApiOperation("查询单个班级信息")
    @GetMapping("findOneClass/{classId}")
    public R findOneClass(@PathVariable String classId) {
        EduClass eduClass = eduClassService.getById(classId);
        if (StringUtils.isEmpty(eduClass)){
            return R.error().message("没有这个班级");
        } else {
            return R.ok().data("item", eduClass);
        }
    }

    @ApiOperation("修改班级信息")
    @PostMapping("uploadClass")
    public R uploadClass(@RequestBody EduClass eduClass){
        boolean b = eduClassService.updateById(eduClass);
        return b ? R.ok() : R.error();
    }

}

